package com.hoo.service.order;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.mapper.BeanMapper;
import com.hoo.common.constant.Constant;
import com.hoo.entity.order.SearchOrderInfoResult;
import com.hoo.entity.order.Ticket;
import com.hoo.entity.order.TicketOrder;
import com.hoo.form.order.TicketForm;
import com.hoo.form.order.TicketOrderForm;
import com.hoo.mapper.order.OrderMapper;
import com.hoo.mapper.order.TicketMapper;
import com.hoo.util.Base64;
import com.hoo.util.DateUtils;
import com.hoo.util.IdUtils;
import com.hoo.util.PaginationSupport;
import com.hoo.util.RandomNum;

@Service
public class TicketOrderService{

	@Autowired
	private OrderMapper orderMapper;

	@Autowired
	private TicketMapper ticketMapper;
	

	/**
	 * 获取所有门票种类
	 * 
	 * @return
	 */
	@Transactional(propagation=Propagation.REQUIRED,readOnly=true)
	public List<Ticket> getAllTickets() {
		return this.ticketMapper.getAllTicket();
	}

	@Transactional(propagation=Propagation.REQUIRED,readOnly=true)
	public Ticket getTicketByTicketNo(String ticketNo) {
		return this.ticketMapper.getTicketByTicketNo(ticketNo);
	}

	/**
	 * 添加门票信息
	 * @param form
	 * @return
	 */
	@Transactional(propagation=Propagation.REQUIRED,readOnly=false)
	public int insertTicket(TicketForm form) {
		Ticket ticket = BeanMapper.map(form, Ticket.class);
		ticket.setTicketNo(Constant.TICKETPRE+DateUtils.getCurrentDate("yyyyMMddhhmmss"));
		ticket.setAddTime(new Date());
		return this.ticketMapper.insertTicket(ticket);
	}

	/**
	 * 修改门票信息
	 * @param form
	 * @return
	 */
	@Transactional(propagation=Propagation.REQUIRED,readOnly=false)
	public int updateTicket(TicketForm form) {
		Ticket ticket = BeanMapper.map(form, Ticket.class);
		ticket.setUpdateTime(new Date());
		return this.ticketMapper.updateTicket(ticket);
	}

	public int updateTicketResidueNum(String ticketNo){
		Ticket ticket = new Ticket();
		ticket.setTicketNo(ticketNo);
		ticket.setUpdateTime(new Date());
		/*WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext();*/
		//TicketMapper ticketMapper = (TicketMapper)Constant.wac.getBean("ticketMapper");
		return this.ticketMapper.updateTicketResidueNum(ticket);
	}
	
	public PaginationSupport<Ticket> searchTicketOrderInfo(TicketForm form) {
		int totalCount = this.ticketMapper.getTicketCount(form);
		List<Ticket> list = this.ticketMapper.getTicketList(form);
		return new PaginationSupport(list, totalCount,form.getiDisplayLength(), form.getiDisplayStart());
	}
	
	
	/**
	 * 网上购票时使用
	 * 
	 * @param form
	 * @return
	 */
	public Map<String, String> insertTicketOrderHanderOnline(TicketOrderForm form) {
		TicketOrder ticketOrder = BeanMapper.map(form, TicketOrder.class);
		ticketOrder.setOrderStatus(3);// 1:支付成功，2.支付失败，3：未支付
		ticketOrder.setTakeOrderStatus(2);//1:已取票，2：未取票
		ticketOrder.setOrderTime(new Date());
		ticketOrder.setScheduleNum(Constant.SCHEDULENUMPRE
				+ new SimpleDateFormat("yyyyMMdd").format(new Date())
				+ RandomNum.createRandomString(5));//生成预约号
		ticketOrder.setOrderNo(Constant.defaultBuyOrderPrefix + IdUtils.flowid());//生成订单号
		if (this.orderMapper.insertTicketOrder(ticketOrder) != 1) {
			return null;
		}
		Map<String,String> map=new HashMap<String, String>();
		map.put("scheduleNum", ticketOrder.getScheduleNum());
		map.put("orderNo", ticketOrder.getOrderNo());
		return map;
	}

	
	/**
	 * 现场购票时使用
	 * 
	 * @param map
	 * @return
	 */
	
	public String insertTicketOrder(Map<String, Object> map) throws Exception {
		TicketOrder ticketOrder = new TicketOrder();
		ticketOrder.setTicketNo(Constant.ticketNo);
		ticketOrder.setOrderNo(Constant.defaultBuyOrderPrefix + IdUtils.flowid());
		ticketOrder.setGroupTicket(2);// 1团体票，2散票
		ticketOrder.setInternetTrading(2);// 1网上购买，2现场购买
		ticketOrder.setLinkmanID(map.get("identityCard").toString());
		ticketOrder.setLinkmanName(map.get("name").toString());
		ticketOrder.setOrderStatus(3);// 1已支付成功，2.支付失败，3：未支付
		ticketOrder.setTakeOrderStatus(1);//1:已取票，2：未取票
		ticketOrder.setOrderTime(new Date());
		ticketOrder.setVisitTime(new Date());
		ticketOrder.setTakeOrderTime(new Date());
		//获取spring管理的bean
		//OrderMapper orderMapper = (OrderMapper)Constant.wac.getBean("orderMapper");
		if(this.orderMapper.insertTicketOrder(ticketOrder)==1){
			return ticketOrder.getOrderNo();
		}
		return null;
	}
	
	/**
	 * 根据预约号查询订单信息
	 * @param scheduleNum
	 * @return
	 */
	public TicketOrder getTicketOrderBySchedule(String scheduleNum) {
		return this.orderMapper.getTicketOrderBySchedle(scheduleNum);
	}

	/**
	 * 查询门票订单列表
	 * @param form
	 * @return
	 */
	public PaginationSupport<TicketOrder> searchTicketOrder(TicketOrderForm form) {
		int totalCount = this.orderMapper.getTicketOrdersCount(form);
		List<TicketOrder> list = this.orderMapper.getTicketOrdersList(form);
		return new PaginationSupport(list, totalCount,form.getiDisplayLength(), form.getiDisplayStart());
	}
	
	/**
	 * 修改门票订单信息
	 * @param ticketOrder
	 * @return
	 */
	@Transactional(propagation=Propagation.REQUIRED,readOnly=false)
	public int updateTicketOrder(TicketOrder ticketOrder){
		return this.orderMapper.updateTicketOrder(ticketOrder);
	}

	
	@Transactional(propagation=Propagation.REQUIRED,readOnly=false)
	public String handleTicketByScheduleNum(String ticketNo,String schedulenum) throws Exception{
		TicketOrder order = this.getTicketOrderBySchedule(schedulenum);
		if(null != order){
			if(1 != order.getTakeOrderStatus()){
				SimpleDateFormat simpleDateFormat=new SimpleDateFormat(Constant.DATE_FORMAT_SIMPLE);
				long date1 = simpleDateFormat.parse(simpleDateFormat.format(new Date())).getTime();
				long date2 = simpleDateFormat.parse(simpleDateFormat.format(order.getVisitTime())).getTime();
				if(date1>=date2){
					Map<String, Object> map=new HashMap<String, Object>();
					map.put("name", order.getLinkmanName());
					map.put("ticketCount", String.valueOf(order.getTicketCount()));
					map.put("scheduleNum",schedulenum);
					map.put("visitTime", simpleDateFormat.format(order.getVisitTime()));
					order.setTakeOrderTime(new Date());
					order.setTakeOrderStatus(1);//已经取票
					//ticketOrderService.updateTicketOrder(order);//修改取票时间
					this.updateTicketResidueNum(ticketNo);
					this.updateTicketOrder(order);//修改订单状态
					return Base64.desEncrypt(JSONObject.fromObject(map).toString(), Base64.KEY);
				}else{
					return Constant.OVERDUE;//预约时间已过
				}
			}else {
				return Constant.DELIVERED;//该预约号已经取过
			}
		}else {
			return Constant.NO_ORDER;//未查到此订单信息
		}
	}
	
	/**
	 * 查询票务信息列表
	 * @param form
	 * @return
	 */
	public PaginationSupport<SearchOrderInfoResult> searchTicketOrderInfo(TicketOrderForm form) {
		int totalCount = this.orderMapper.getTicketOrdersInfoCount(form);
		List<TicketOrder> list = this.orderMapper.getTicketOrdersInfoList(form);
		return new PaginationSupport(list, totalCount,form.getiDisplayLength(), form.getiDisplayStart());
	}

	
	public Ticket getTicketOnUse() throws Exception {
		/*WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext();
		TicketMapper ticketMapper = (TicketMapper)wac.getBean("ticketMapper");*/
		//TicketMapper ticketMapper = (TicketMapper)Constant.wac.getBean("ticketMapper");
		return this.ticketMapper.getTicketOnUse();
	}
	
	public void test(){
		
	}

	@Transactional(propagation=Propagation.REQUIRED,readOnly=false)
	public int updateAllTicket(int i) throws Exception{
		return ticketMapper.updateAllTicketStatus(i);
	}

}
